using EasyAdmin.Admin.Application.Services;
using EasyAdmin.Admin.Entity.Entities;
using EasyAdmin.Admin.Entity.Common;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace EasyAdmin.Admin.WebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class SysUserController : ControllerBase
    {
        private readonly ISysUserService _userService;

        public SysUserController(ISysUserService userService)
        {
            _userService = userService;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        [HttpPost("login")]
        public async Task<ApiResult<object>> Login([FromBody] LoginRequest request)
        {
            var result = await _userService.LoginAsync(request.Username, request.Password);
            if (!result.Success)
                return ApiResult<object>.Fail(result.Message);

            return ApiResult<object>.Ok(new { token = result.Token }, result.Message);
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        [HttpPost]
        [Authorize]
        public async Task<ApiResult> CreateUser([FromBody] SysUser user)
        {
            var result = await _userService.CreateUserAsync(user);
            if (!result.Success)
                return ApiResult.Fail(result.Message);

            return ApiResult.Ok(result.Message);
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        [HttpPut("{id}")]
        [Authorize]
        public async Task<ApiResult> UpdateUser(int id, [FromBody] SysUser user)
        {
            if (id != user.Id)
                return ApiResult.BadRequest("用户ID不匹配");

            var result = await _userService.UpdateUserAsync(user);
            if (!result.Success)
                return ApiResult.Fail(result.Message);

            return ApiResult.Ok(result.Message);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        [HttpDelete("{id}")]
        [Authorize]
        public async Task<ApiResult> DeleteUser(int id)
        {
            var result = await _userService.DeleteUserAsync(id);
            if (!result.Success)
                return ApiResult.Fail(result.Message);

            return ApiResult.Ok(result.Message);
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        [HttpGet("{id}")]
        [Authorize]
        public async Task<ApiResult<SysUser>> GetUser(int id)
        {
            var user = await _userService.GetUserByIdAsync(id);
            if (user == null)
                return ApiResult<SysUser>.NotFound("用户不存在");

            return ApiResult<SysUser>.Ok(user);
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        [HttpGet("list")]
        [Authorize]
        public async Task<ApiResult<object>> GetUserList([FromQuery] string keyword = "", [FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 10)
        {
            var result = await _userService.GetUserListAsync(keyword, pageIndex, pageSize);
            return ApiResult<object>.Ok(new { items = result.Items, total = result.Total });
        }

        /// <summary>
        /// 获取用户角色
        /// </summary>
        [HttpGet("{id}/roles")]
        [Authorize]
        public async Task<ApiResult<object>> GetUserRoles(int id)
        {
            var roles = await _userService.GetUserRolesAsync(id);
            return ApiResult<object>.Ok(roles);
        }

        /// <summary>
        /// 设置用户角色
        /// </summary>
        [HttpPost("{id}/roles")]
        [Authorize]
        public async Task<ApiResult> SetUserRoles(int id, [FromBody] List<int> roleIds)
        {
            var result = await _userService.SetUserRolesAsync(id, roleIds);
            if (!result.Success)
                return ApiResult.Fail(result.Message);

            return ApiResult.Ok(result.Message);
        }
    }

}